package com.example.demo.service.serviceImpl;

import com.example.demo.entity.Author;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Post;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.requestJson.InsertPostReq;
import com.example.demo.entity.requestJson.UpdatePostReq;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.AuthorMapper;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.AuthorPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * @author : 洪俊新
 * @date : 2021/3/12 17:44
 * @description:
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class AuthorPostServiceImpl implements AuthorPostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private AuthorMapper authorMapper;

    /**
     * 发布新帖子（在创作中心）
     * */
    @Override
    public BackResponseEntity<Object> insertPost(InsertPostReq insertPostReq) {
        String authorId = null;
        String postName = null;
        String postContent = null;
        String oneLabel = null;
        String twoLabel = null;

        if (!ObjectUtils.isEmpty(insertPostReq.getAuthorId())) {
            authorId = insertPostReq.getAuthorId()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(insertPostReq.getPostName())) {
            postName = insertPostReq.getPostName()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(insertPostReq.getPostContent())) {
            postContent = insertPostReq.getPostContent()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(insertPostReq.getPostOnelabel())) {
            oneLabel = insertPostReq.getPostOnelabel()
                    .trim();
        }
        if (!ObjectUtils.isEmpty(insertPostReq.getPostTwolabel())) {
            twoLabel = insertPostReq.getPostTwolabel()
                    .trim();
        }

        if (ObjectUtils.isEmpty(authorId) || ObjectUtils.isEmpty(postName) || ObjectUtils.isEmpty(postContent) || ObjectUtils.isEmpty(oneLabel)) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }
        Author author = authorMapper.selectById(authorId);
        Post post = new Post();
        post.setAuthorId(authorId);
        post.setAuthorName(author.getAuthorNickname());
        post.setPostName(postName);
        post.setPostContent(postContent);
        post.setPostOnelabel(oneLabel);
        post.setPostTwolabel(twoLabel);
        post.setCommentCount(0);
        post.setFavoriteCount(0);
        post.setLikeCount(0);
        post.setCreatTime(LocalDateTime.now());

        int i = postMapper.insert(post);
        if (i > 0) {
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "发布新帖子成功");
        } else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1044, "发布帖子失败");
        }
    }
    /**
     * 编辑帖子信息
     * */
    @Override
    public BackResponseEntity<Object> updatePost(UpdatePostReq updatePostReq){
        String postId = null;
        String postName = null;
        String postContent = null;
        String oneLabel = null;
        String twoLabel = null;

        if(!ObjectUtils.isEmpty(updatePostReq.getPostId())){
            postId = updatePostReq.getPostId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePostReq.getPostName())){
            postName = updatePostReq.getPostName()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePostReq.getPostContent())){
            postContent = updatePostReq.getPostContent()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePostReq.getPostOnelabel())){
            oneLabel = updatePostReq.getPostOnelabel()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(updatePostReq.getPostTwolabel())){
            twoLabel = updatePostReq.getPostTwolabel()
                    .trim();
        }
        if(ObjectUtils.isEmpty(postId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        Post post = new Post();
        post.setPostId(postId);
        post.setPostContent(postContent);
        post.setPostName(postName);
        post.setPostOnelabel(oneLabel);
        post.setPostTwolabel(twoLabel);
        post.setUpdateTime(LocalDateTime.now());

        int i = postMapper.updateById(post);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "编辑帖子信息成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1045, "编辑帖子信息失败");
        }
    }
    @Override
    public BackResponseEntity<Object> deletePost(DeleteReq deleteReq){
       String postId = null;

       if(ObjectUtils.isEmpty(deleteReq.getId())){
           return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
       }
       if(!ObjectUtils.isEmpty(deleteReq.getId())){
           postId = deleteReq.getId()
                   .trim();
       }
       int i = postMapper.deletePostById(postId);

       if(i > 0){
           return BackResponseEntity.success(ErrCodeEnum.unknown_0, "删除成功");
       }else {
           return BackResponseEntity.success(ErrCodeEnum.unknown_1046, "删除帖子失败");
       }
    }
}
